<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>保险登记</title>
    <link th:include="/comm/header :: header"/>
    <script type="text/javascript">
        var day2 = new Date();
        day2.setTime(day2.getTime());
        function formatOp(val, row, index) {
            var str = '';
            var year1=day2.getFullYear();
            var month1=day2.getMonth()+1;
            var day1=day2.getDate();
            var date2=new Date(row.etime);
            var year=date2.getFullYear();
            var month=date2.getMonth()+1;
            var day=date2.getDate();
            var day3=day1-day;
            if(day2>date2){
                str += $.formatString('<span class="role-easyui-linkbutton-dao error l-btn l-btn-small l-btn-plain">已到期</span>');
                str += $.formatString('<a href="javascript:void(0)" class="role-easyui-linkbutton-del error l-btn l-btn-small l-btn-plain"   onclick="deleteFun(\'{0}\');" >删除</a>', row.id);
            }else if(year1==year&&month1==month&&day3<=3){
                str += $.formatString('<span class="role-easyui-linkbutton-jdao error l-btn l-btn-small l-btn-plain">即将到期</span>');
            }
            return str;
        }

        $(function () {
            $("#carPlatenum").combobox({
                onChange: function () {
                    var category = $("#carPlatenum").combobox("getText");
                    //司机list
                    $.ajax({
                        url: '/car/driver/getlist',
                        type: 'get',
                        datatype: 'json',
                        data: {carPlatenum: category},
                        success: function (data) {
                            console.log(data)
                            $("#dirName").val(data[0].dirName);
                            $("#dirTelephone").val(data[0].dirTelephone)
                        }
                    });
                    //队长list
                    $.ajax({
                        url: '/car/caption/elist',
                        type: 'get',
                        datatype: 'json',
                        data: {carPlatenum: category},
                        success: function (data) {
                            $("#capName").val(data[0].capName);
                            $("#capTelephone").val(data[0].capTelephone)
                        }
                    })
                }
            })
        });

        //搜索
        function searchGuarantee() {
            $("#dg").datagrid('load', {
                "idCard": $("#idcard").val()
            });
        }

        //添加
        function openOverflowListGoodsAddDialog() {
            $('#fm').form(resetAll());
            $("#fm").form("clear");
            $("#dlg2").dialog("open").dialog("setTitle", "保险信息");
        }

        //清空
        function resetAll() {
            $("#fm").find('input[type=text],input[type=date],textarea,input[type=hidden]').each(function () {
                $(this).val('');
            });
        }

        //修改
        function editFun(index) {
            $("#dg").datagrid('selectRow', index);
            var row = $("#dg").datagrid("getSelected");
            $("#fm").form("clear");
            $("#fm").form("load", row);
            $("#dlg2").dialog("open").dialog("setTitle", "修改");
        }

        //删除
        function deleteFun(id) {
            $.messager.confirm("系统提示", "您确定要删除这条数据吗?", function (r) {
                if (r) {
                    $.post("/ins/delete", {id: id}, function (result) {
                        if (result.flag) {
                            message("删除成功！");
                            $("#dg").datagrid("reload");
                        } else {
                            message(result.message)
                        }
                    }, "json");
                }
            });
        }


        //保存
        function saveGuarantees() {
            $("#company").val($("#companyName").val());
            $("#ctype").val($("#ctypeName").val());
            $("#ccycle").val($("#ccycleName").val());

            $("#fm").form("submit", {
                url: "/ins/addInsuranInfo",
                onSubmit: function () {
                    return $(this).form("validate");
                },
                success: function (result) {
                    console.log(result);
                    if (result.flag) {
                        message("保存成功");
                    }
                    $("#dg").datagrid("reload");
                    $("#dlg2").dialog("close");
                }
            });
        }

        //关闭
        function closeGoodsChooseDialog() {
            $("#dlg2").dialog("close");
        }

        function myformatter(date){
            var y = "";
            var m = date.getMonth()+1;
            var d = date.getDate();
            if (year=="一年"){
                y=date.getFullYear()+1;
            }
            if (year=="二年"){
                y=date.getFullYear()+2;
            }
            if (year=="三年"){
                y=date.getFullYear()+3;
            }
            var time=y+"-"+m+"-"+d;
            $('#etime').datebox('setValue', time);
            console.log(y+"=="+m+"=="+d+"===="+year);
        }


        var year="";
        function getInsurance(record){
            year=record.cname;
        }

        $('#dirName').textbox({
            readonly: "flase"
        })
    </script>
</head>
<body style="margin: 1px">
<!--<shiro:hasPermission name="insurance:delete">-->
<input type="hidden" id="insurancedelete"/>
<!--</shiro:hasPermission>-->
<!-- 主页显示 -->
<table id="dg" title="保险信息" class="easyui-datagrid"
       fitColumns="true" pagination="true" rownumbers="true" singleSelect="true" method="get"
       url="/ins/all" fit="true" toolbar="#tb">
    <thead>
    <th data-options="field:'ck',checkbox:true"></th>
    <th field="id" hidden></th>
    <th field="idcard" width="30" align="center">车牌号</th>
    <th field="driverName" width="30" align="center">司机姓名</th>
    <th field="driverPhone" width="50" align="center">司机手机</th>
    <th field="captainName" width="30" align="center">队长姓名</th>
    <th field="captainPhone" width="50" align="center">队长手机</th>
    <th field="ctypeName" width="30" align="center">保单类型</th>
    <th field="companyName" width="30" align="center">保险公司</th>
    <th field="cname" width="30" align="center">保险名称</th>
    <th field="cno" width="60" align="center">保单号</th>
    <th field="stime" width="50" align="center">投保时间</th>
    <th field="ccycleName" width="30" align="center">投保周期</th>
    <th field="etime" width="50" align="center">到期时间</th>
    <th field="op" width="60" align="center" formatter="formatOp">操作</th>
    </thead>
</table>

<div id="tb">
    <div style="display: inline-block;padding: 2px">
        <a href="javascript:openOverflowListGoodsAddDialog()"
           class="easyui-linkbutton success l-btn l-btn-small l-btn-plain" iconCls="fa fa-plus-square"
           plain="true">添加</a>
    </div>
    <div style="display: inline-block;float: right">
        &nbsp;车牌号：&nbsp;<input class="easyui-textbox" data-options="iconCls:'fa fa-car',iconAlign:'left'"
                               prompt="请输入车牌号" id="idcard"
                               size="20" onkeydown="if(event.keyCode==13) searchGuarantee()"/>
        <a href="javascript:searchGuarantee()" class="easyui-linkbutton primary l-btn l-btn-small l-btn-plain"
           iconCls="icon-search" plain="true">搜索</a>
    </div>
</div>

<div id="dlg2" class="easyui-dialog" style="width: 500px;height: 300px;padding: 5px 5px"
     closed="true" buttons="#dlg-buttons2">
    <div style="padding-left: 12px;padding-top: 10px">
        <form id="fm" method="post">
            <table cellspacing="8px">
                <input type="hidden" name="id"/>
                <tr>
                    <td>车牌号：</td>
                    <td>
                        <input type="hidden" id="idcard1" name="idcard"/>
                        <input class="easyui-combobox" id="aidcard" name="idcard1" style="width:100%;" data-options="url:'/ins/getInsuList',method:'get',
                            valueField:'id',textField:'idcard',panelHeight:'auto',label: '',labelPosition: 'top',onSelect: function(newValue){
                                    $.post('/ins/getInsuListData',{'id':newValue.id},function(data){
                                    console.log(data.data);
                                        $('#driverName').val(data.data.driverName);
                                        $('#driverPhone').val(data.data.driverPhone);
                                        $('#captainName').val(data.data.captainName);
                                        $('#captainPhone').val(data.data.captainPhone);

                                        $('#idcard1').val(newValue.idcard);
                                        $('#captianName').val(data.data.captainName);
                                        $('#captianPhone').val(data.data.captainPhone);
                                    });
                            }" required/>
                    <td>保单号：</td>
                    <td>
                        <input type="text" id="cno" name="cno" class="easyui-validatebox "
                               required="true"/>
                    </td>
                    </td>
                </tr>
                <tr>
                    <td>司机姓名：</td>
                    <td>
                        <input type="text" id="driverName" name="driverName" class="easyui-validatebox"
                               readonly="readonly"/>
                    </td>
                    <td>司机手机：</td>
                    <td>
                        <input type="text" id="driverPhone" name="driverPhone" class="easyui-validatebox"
                               readonly="readonly"/>
                    </td>
                </tr>
                <tr>
                    <td>队长姓名：</td>
                    <td>
                        <input type="hidden" id="captianName" name="captianName"/>
                        <input type="text" id="captainName" name="captainName" class="easyui-validatebox"
                               readonly="readonly"/>
                    </td>
                    <td>队长手机：</td>
                    <td>
                        <input type="hidden" id="captianPhone" name="captianPhone"/>
                        <input type="text" id="captainPhone" name="captainPhone" class="easyui-validatebox"
                               readonly="readonly"/>
                    </td>
                </tr>
                <tr>
                    <td>保险公司：</td>
                    <td align="left">
                        <input type="hidden" name="company" id="company"/>
                        <input id="companyName" class="easyui-combobox" name="companyName"
                               data-options="valueField:'id',editable:false,textField:'cname',url:'/ins/getInsuranceCompany'"/>
                    </td>
                    <td>保险类型：</td>
                    <td>
                        <input type="hidden" name="ctype" id="ctype"/>
                        <input id="ctypeName" class="easyui-combobox" name="ctypeName" data-options=" valueField:'id',editable:false,textField:'cname',url:'/ins/getInsuranceType', onSelect: function(rec){
                        }"/>
                    </td>
                </tr>
                <tr>
                    <td>保单名称：</td>
                    <td>
                        <select class="easyui-combobox" data-options="editable:false" name="cname" label=""
                                labelPosition="top" style="width:100%;">
                            <option value="年金保险">年金保险</option>
                            <option value="健康保险">健康保险</option>
                            <option value="意外伤害保险">意外伤害保险</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>投保周期：</td>
                    <td>
                        <input type="hidden" name="ccycle" id="ccycle"/>
                        <input id="ccycleName" class="easyui-combobox" name="ccycleName"
                               data-options="valueField:'id',textField:'cname',url:'/ins/getInsurancecycle',onSelect:getInsurance"/>
                    </td>
                    <td>投保时间：</td>
                    <td>
                        <input id="stime" name="stime" class="easyui-datebox" style="width: auto"
                               data-options="onSelect:myformatter" />
                    </td>
                    <td>
                        <input type="hidden" id="etime" name="etime" class="easyui-datebox"/>
                    </td>
                </tr>
            </table>
        </form>
    </div>
</div>

<div id="dlg-buttons2">
    <a href="javascript:saveGuarantees()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
    <a href="javascript:closeGoodsChooseDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>
<script type="text/javascript">
    $(function () {
        $("#dg").datagrid({
            onLoadSuccess: function (data) {
                $('.role-easyui-linkbutton-del').linkbutton({text: '删除', plain: true, iconCls: 'icon-remove'});
                $('.role-easyui-linkbutton-dao').linkbutton({text: '已到期', plain: true, iconCls: 'icon-remove'});
                $('.role-easyui-linkbutton-jdao').linkbutton({text: '即将到期', plain: true, iconCls: 'icon-remove'});
                $('#dg').datagrid('fixRowHeight');
            }
        });
    })
</script>
</body>
</html>